/**
 * 
 */
package org.statewalker.parser.xhtml.validator;

import java.util.HashSet;
import java.util.Set;

import org.statewalker.parser.xhtml.grammar.XHTMLDict;
import org.statewalker.parser.xhtml.grammar.XHTMLGrammar;
import org.statewalker.validator.Validator;
import org.statewalker.validator.ValidatorFactory;
import org.statewalker.validator.ValidatorGrammarConfig;

public class XHTMLValidatorFactory extends ValidatorFactory {

    protected Set<String> fSpaceContainers;

    protected XHTMLGrammar fXhtmlGrammar;

    public XHTMLValidatorFactory() {
        super();
    }

    /**
     * @param stateKey
     * @return <code>true</code> if a state with the given key accepts white
     *         spaces
     */
    public boolean acceptSpaces(String stateKey) {
        return fSpaceContainers.contains(stateKey);
    }

    @Override
    protected void configureDictionaries() {
    }

    @Override
    protected void configureTransitions(ValidatorGrammarConfig config) {
        fSpaceContainers = new HashSet<String>();
        fXhtmlGrammar = new XHTMLGrammar(config) {
            @Override
            protected void markKeyAsAcceptingSpaces(String key) {
                fSpaceContainers.add(key);
            }
        };
        fXhtmlGrammar.init();
        // We don't allow to put spaces directly in the body...
        fSpaceContainers.remove(XHTMLDict.BODY);
    }

    public XHTMLGrammar getXhtmlGrammar() {
        return fXhtmlGrammar;
    }

    @Override
    public Validator newValidator() {
        return new XHTMLValidator(this);
    }

}